﻿using BBCore.Core.Models.Admins;
using BBCore.Server.Data;
using BootstrapBlazor.Components;
using DocumentFormat.OpenXml.Drawing.Charts;

namespace BBCore.Server.Components.Pages.Admins
{
    public partial class DictManager
    {
        /// <summary>
        /// 主表数据
        /// </summary>
        public List<SysDictType>? master { get; set; }
        /// <summary>
        /// 从表数据
        /// </summary>
        private List<SysDictData>? detailData { get; set; }
        /// <summary>
        /// 选中的主表行
        /// </summary>
        private SysDictType? selectedDictType;

        /// <summary>
        /// 主表数据
        /// </summary>
        /// <returns></returns>
        private async Task<List<SysDictType>> LoadMasterData()
        {
            var master_data = await SysDictType.Select.OrderByDescending(t => t.Id).ToListAsync();
            return master_data;
        }
        /// <summary>
        /// 从表数据
        /// </summary>
        /// <returns></returns>
        private async Task LoadDetailData()
        {
            if (selectedDictType != null)
            {
                detailData = null;
                var x = selectedDictType.Id;
                detailData = await SysDictData.Where(t => t.TypeId == selectedDictType.Id).ToListAsync();
            }
            else
            {
                detailData = new List<SysDictData>();
            }
        }
        private async Task<bool> OnSaveSysDictTypeAsync(SysDictType item, ItemChangedType changedType)
        {

            if (changedType == ItemChangedType.Add)
            {
                // 新增
                await item.InsertAsync();
                selectedDictType = null;
                StateHasChanged();
            }
            else
            {
                //更新
                await item.UpdateAsync();
            }
            // await LoadMasterData();
            //await InvokeAsync(StateHasChanged);
            return await Task.FromResult(true);
        }
        private async Task<bool> OnDeleteSysDictTypeAsync(IEnumerable<SysDictType> items)
        {
            var tmp = items.FirstOrDefault();
            await tmp!.DeleteAsync();
            selectedDictType = null;
            StateHasChanged();
            return await Task.FromResult(true);
        }
        private async Task<QueryData<SysDictType>> OnQuerySysDictTypeAsync(QueryPageOptions options)
        {
            IEnumerable<SysDictType> master = await LoadMasterData(); // await SysDictType.Select.OrderBy(t => t.Id).ToListAsync();
            if (!string.IsNullOrEmpty(options.SearchText))
            {
                master = master.Where(t => t.TypeName.Contains(options.SearchText) || t.TypeCode.Contains(options.SearchText));
            }
            // 设置记录总数
            var total = master.Count();
            //内存分页
            master = master.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();
            selectedDictType = null;
            StateHasChanged();
            return await Task.FromResult(new QueryData<SysDictType>()
            {
                Items = master,
                TotalCount = total,
                IsSearch = true
            });


        }

        /// <summary>
        /// 选中主表行时触发,得到当前选中行的数据
        /// </summary>
        /// <param name="SysDictType_row"></param>
        /// <returns></returns>
        private async Task OnClickSysDictTypeRow(SysDictType SysDictType_row)
        {

            selectedDictType = SysDictType_row;
            await LoadDetailData();
            StateHasChanged();

        }
        private bool SysDictDataKey;
        private async Task<bool> OnSaveSysDictDataAsync(SysDictData item, ItemChangedType changedType)
        {

            if (changedType == ItemChangedType.Add)
            {
                item.TypeId = selectedDictType!.Id;
                // 新增
                await item.InsertAsync();
                SysDictDataKey = !SysDictDataKey;

            }
            else
            {
                //更新
                await item.UpdateAsync();
            }
            await LoadDetailData();
            await InvokeAsync(StateHasChanged);
            return await Task.FromResult(true);
        }
        private async Task<bool> OnDeleteSysDictDataAsync(IEnumerable<SysDictData> items)
        {
            var tmp = items.FirstOrDefault();
            await tmp!.DeleteAsync();
            await LoadDetailData();
            StateHasChanged();
            return await Task.FromResult(true);
        }

    }
}
